package com.testing.redboy.fragment;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.testing.redboy.FirstActivity;
import com.testing.redboy.R;
import com.testing.redboy.bean.SearchBean;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.List;

public class SearchFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    private String mParam1;
    private String mParam2;
    private TextView s_count;
    private ListView lv;
    private TextView back;


    public SearchFragment() {
    }

    public static SearchFragment newInstance(String param1, String param2) {
        SearchFragment fragment = new SearchFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_search, container, false);

        initView(v);

        setData();

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), FirstActivity.class);
                startActivity(intent);
            }
        });

        return v;
    }

    private void initView(View v) {
        s_count = (TextView) v.findViewById(R.id.s_count);
        lv = (ListView) v.findViewById(R.id.limit_lv);
        back = (TextView) v.findViewById(R.id.limit_back);
    }

    public void setData() {
        String u = "http://120.27.126.103:8080/ECServer_D/search";
        RequestParams params = new RequestParams(u);
        x.http().get(params, new Callback.CommonCallback<String>() {

            private int list_count;
            private List<SearchBean.ProductlistBean> arr;

            @Override
            public void onSuccess(String result) {
                Gson g = new Gson();
                SearchBean sb = g.fromJson(result, SearchBean.class);

                list_count = sb.list_count;
                arr = sb.productlist;
                s_count.setText(list_count + "条");
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {


                lv.setAdapter(new Myba(getActivity(), arr));


            }
        });
    }

    class Myba extends BaseAdapter {

        Context context;
        List<SearchBean.ProductlistBean> al;


        public Myba(Context context, List<SearchBean.ProductlistBean> al) {
            this.context = context;
            this.al = al;
        }

        @Override
        public int getCount() {
            return al.size();
        }

        @Override
        public Object getItem(int position) {
            return al.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Viewh vh;
            if (convertView == null) {
                convertView = View.inflate(context, R.layout.search_item, null);
                vh = new Viewh();
                vh.imimim = (ImageView) convertView.findViewById(R.id.imimim);
                vh.gname = (TextView) convertView.findViewById(R.id.gname);
                vh.red_p = (TextView) convertView.findViewById(R.id.red_p);
                vh.hei_p = (TextView) convertView.findViewById(R.id.hei_p);
                vh.pinglun_num = (TextView) convertView.findViewById(R.id.pinglun_num);
                convertView.setTag(vh);
            } else {
                vh = (Viewh) convertView.getTag();
            }

            x.image().bind(vh.imimim, "http://120.27.126.103:8080/ECServer_D/" + al.get(position).pic);
            vh.gname.setText(al.get(position).name);
            vh.red_p.setText("￥:" + al.get(position).price);
            vh.hei_p.setText("￥:" + al.get(position).marketprice);
            vh.pinglun_num.setText("已经有" + al.get(position).comment_count + "人评价");

            return convertView;
        }

        class Viewh {
            ImageView imimim;
            TextView gname;
            TextView red_p;
            TextView hei_p;
            TextView pinglun_num;
        }
    }

}
